package com.bidevalution.business.checkJob;

import com.alibaba.dubbo.common.json.JSON;
import com.bidevalution.business.exception.CheckException;
import com.bidevalution.business.factory.CheckJobBase;
import com.bidevalution.business.service.BeSectionExpertInfoService;
import com.bidevalution.business.service.BeSectionWarnMsgService;
import com.bidevalution.business.vo.CheckJobVo;
import com.bidevalution.dao.entity.BeSectionExpertInfo;
import com.bidevalution.dao.entity.BeSectionWarnMsg;
import com.bidevalution.dao.entity.CheckJob;
import com.bidevalution.dao.entity.ExpertWarnMsgVo;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Log4j
@Component
public class JiShuScoreCheckJob extends CheckJobBase {
    public static JiShuScoreCheckJob ziXinScoreCheckJob;

    /**
     * 初始化当前类
     */
    @PostConstruct
    public void init() {
        ziXinScoreCheckJob = this;
    }

    @Autowired
    private BeSectionWarnMsgService beSectionWarnMsgService;
    @Autowired
    private BeSectionExpertInfoService beSectionExpertInfoService;

    @Override
    public void check(CheckJob checkJob) throws Exception,CheckException {
        String sectionId = checkJob.getSectionId();
        String sectionStepId = checkJob.getSectionStepId();
        List<ExpertWarnMsgVo> list = ziXinScoreCheckJob.beSectionWarnMsgService.getExpertWarnMsgDetail(sectionStepId, sectionId);
        CheckJobVo checkJobVo = new CheckJobVo();
        if (list.size() > 0) {
            checkJobVo.setMsg("超过 30% 有专家需要进行重评");
            for (ExpertWarnMsgVo e: list) {
                checkJobVo.add(e);
            }

            // 查找出来所有的专家
            List<BeSectionExpertInfo> sectionExperts = ziXinScoreCheckJob.beSectionExpertInfoService.getExpertListBySectionId(sectionId);

            List<BeSectionWarnMsg> warnMsgList = ziXinScoreCheckJob.beSectionWarnMsgService.getBeSectionWarnMsgList(sectionStepId, null);
            Map<String, String> warnMsgMap = new HashMap<>();

            for (BeSectionWarnMsg item: warnMsgList) {
                warnMsgMap.put(item.getExpertId(), item.getExpertId());
            }
            checkJobVo.setExperTotal(sectionExperts.size());
            for (BeSectionExpertInfo item: sectionExperts) {
                String expId = item.getSeqId();
//                if (warnMsgMap.get(expId) != null) {
//                    continue;
//                }

                BeSectionWarnMsg beSectionWarnMsg = new BeSectionWarnMsg(checkJob);
                if (!StringUtils.isEmpty(checkJobVo.getFailureMap().get(expId))) {
                    checkJobVo.setMsg("你有投标单位总评分偏离平均分30%以上，请确认是否有误！");
                } else {
                    checkJobVo.setMsg("有专家总评分偏离平均分30%以上”，必须等有偏离的专家操作确认无误后或者修改分数重新提交才能下一步，请等待");
                }
                beSectionWarnMsg.setExpertId(expId);
                beSectionWarnMsg.setMsg(JSON.json(checkJobVo));
                ziXinScoreCheckJob.beSectionWarnMsgService.insertWarMsg(beSectionWarnMsg);
            }


            throw new CheckException("检验超过 30% ", new ArrayList<>(checkJobVo.getFailureMap().keySet()));
        }

    }
}
